package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.dao.OrderSettingDao;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderService;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 体检预约服务
 */
@Service(interfaceClass = OrderService.class)
@Transactional
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderSettingDao orderSettingDao;

    @Autowired
    private MemberDao memberDao;

    @Autowired
    private OrderDao orderDao;

    @Override
    public Result order(Map map) throws Exception {
        //1.检查用户所选择的预约日期是否有预约设置
        String orderDate = (String) map.get("orderDate");
        OrderSetting orderSetting = orderSettingDao.findByOrderDate(DateUtils.parseString2Date(orderDate));
        if (orderSetting == null) {
            //没有预约设置，无法预约
            return new Result(false, MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }

        //2.检查预约日期是否已经约满
        if (orderSetting.getReservations() >= orderSetting.getNumber()) {
            //约满了，无法预约
            return new Result(false, MessageConstant.ORDER_FULL);
        }

        //3.判断用户是否在同一天预约了同一个套餐（重复）
        String telephone = (String) map.get("telephone");   //页面输入手机号
        Member member = memberDao.findByTelephone(telephone);   //查询是否是会员
        if (member != null) {
            //判断是否重复预约
            Integer memberId = member.getId();  //会员ID
            Date order_Date = DateUtils.parseString2Date(orderDate);    //预约日期
            String setmealId = (String) map.get("setmealId");   //套餐ID
            Order order = new Order(memberId, order_Date, Integer.parseInt(setmealId));
            //查询Order表是否已经有预约条
            List<Order> list = orderDao.findByCondition(order);
            if (list != null && list.size() > 0) {
                //重复预约，无法再次预约
                return new Result(false, MessageConstant.HAS_ORDERED);
            }
        } else {

            //4.不是会员，则自动注册
            member = new Member();
            member.setName((String) map.get("name"));
            member.setPhoneNumber(telephone);
            member.setIdCard((String) map.get("idCard"));
            member.setSex((String) map.get("sex"));
            member.setRegTime(new Date());
            memberDao.add(member);
        }

        //5.预约成功，更新当日已预约人数
        Order order = new Order();
        order.setMemberId(member.getId());  //设置会员ID
        order.setOrderDate(DateUtils.parseString2Date(orderDate));  //预约日期
        order.setOrderType((String) map.get("orderType"));  //预约类型
        order.setOrderStatus(Order.ORDERSTATUS_NO); //到诊状态(未到诊)
        order.setSetmealId(Integer.parseInt((String) map.get("setmealId")));    //套餐ID
        orderDao.add(order);

        orderSetting.setReservations(orderSetting.getReservations() + 1);
        orderSettingDao.editReservationsByOrderDate(orderSetting);
        return new Result(true, MessageConstant.ORDER_SUCCESS, order.getId());
    }

    //根据预约ID查询预约相关信息(体检人姓名、预约日期、套餐名称、预约类型【4张表】)
    @Override
    public Map findById(Integer id) throws Exception {
        Map map = orderDao.findById4Detail(id);
        if(map != null){
            //处理日期格式
            Date orderDate = (Date) map.get("orderDate");
            map.put("orderDate",DateUtils.parseDate2String(orderDate));
        }
        return map;
    }
}
